//begin of moviews.js
var pages = pages || {};
(function(p) {
  p.movies = {
    views: {
      new: {
        init: function() {
          $('.submit').on('click', function() {
            _submitShare();
          });
          //中文名设置光标显示
          $("input[name='cn_name']").focus();
        }
      },
      show: {
        init: function() {
          $('.add-comment > button').on('click', function() {
            $.ajax({
              url: "/api.asp",
              type: "post",
              data: {
                'action': 'movies.newcomment',
                'sharemovie_id': $('#movie_id').val(),
                'content': $('#comment_edit').val(),
                'nick_name': $('#nick_name').val()
              },
              success: function(data) {
                var oRet = pages.common.parseJSON(data);
                var content = $('<div />').text(oRet.content).html();
                content = content.replace(/\n/g, "<br />");

                var data_struct = {
                  'tagName': 'div',
                  'attributes': {
                    'class': 'panel-body narrow-gap'
                  },
                  'children': [{
                    'tagName': 'div',
                    'attributes': {
                      'class': 'comment-text'
                    },
                    'content': content
                  }, {
                    'tagName': 'div',
                    'attributes': {
                      'class': 'comment-operation'
                    },
                    'children': [{
                      'tagName': 'ul',
                      'attributes': {
                        'class': 'list-inline'
                      },
                      'children': [{
                        'tagName': 'li',
                        'attributes': {
                          'class': 'thumbs-up',
                          'data-id': oRet.id.toString()
                        },
                        'children': [{
                          'tagName': 'i',
                          'attributes': {
                            'class': 'fa fa-thumbs-up operation'
                          },
                          'children': [{
                            'tagName': 'span',
                            'attributes': {
                              'class': 'num'
                            },
                            'content': '0'
                          }]
                        }]
                      }, {
                        'tagName': 'li',
                        'attributes': {
                          'class': 'thumbs-down',
                          'data-id': oRet.id.toString()
                        },
                        'children': [{
                          'tagName': 'i',
                          'attributes': {
                            'class': 'fa fa-thumbs-down operation'
                          },
                          'children': [{
                            'tagName': 'span',
                            'attributes': {
                              'class': 'num'
                            },
                            'content': '0'
                          }]
                        }]
                      }, {
                        'tagName': 'li',
                        'attributes': {
                          'class': 'sub-text'
                        },
                        content: oRet.created_at
                      }, {
                        'tagName': 'li',
                        'attributes': {
                          'class': 'sub-text'
                        },
                        content: oRet.nick_name
                      }]
                    }]
                  }]
                };

                var oDiv = pages.common.json2html(data_struct);
                if ($('.narrow-gap').length > 0) {
                  $(oDiv).insertAfter('.narrow-gap:last');
                } else {
                  $(oDiv).insertAfter('.comment-label:last');
                }

                $('#comment_edit').val('');
                $('#comment_edit').focus();
              }
            });
          });

          $(document).delegate('.thumbs-up', 'click', function() {
            var that = this;
            $.ajax({
              url: "/api.asp",
              type: "post",
              data: {
                'action': 'movies.comment.like',
                'id': this.getAttribute('data-id')
              },
              success: function(data) {
                if (data.length > 0) {
                  $(that).find('span').html(data);
                }
              }
            });
          });

          $(document).delegate('.thumbs-down', 'click', function() {
            var that = this;
            $.ajax({
              url: "/api.asp",
              type: "post",
              data: {
                'action': 'movies.comment.dislike',
                'id': this.getAttribute('data-id')
              },
              success: function(data) {
                if (data.length > 0) {
                  $(that).find('span').html(data);
                }
              }
            });
          });
        }
      }
    }
  };
  //local functions
  //inner functions

  function _submitShare() {
    var formCode = $('.new_share').serialize();
    var cn_name = $("[name='cn_name']").val();
    var en_name = $("[name='en_name']").val();

    if (!cn_name && !en_name) {
      alert('中英文至少选择一个填写！');
      return;
    };

    //调整发布按钮的文字
    $('.submit').attr('disabled', 'disabled');
    $('.submit').val('正在提交..')
    $.ajax({
      url: "/api.asp",
      type: "post",
      data: 'action=movies.new&' + formCode,
      success: function(data) {
        var oRet = pages.common.parseJSON(data);
        if (oRet) {
          if (oRet.ref == "ok") {
            if (oRet.id == '-1') {
              pages.common.control.goTo('');
            } else {
              pages.common.control.goTo('show.asp?id=' + oRet.id);
            }
          }
        }
        $('.submit').removeAttr('disabled');
        $('.submit').val('发布分享')
      }
    });
  }
})(pages);
//end of create.js
